|
If you can't view the Datasheet, Please click here to try to view without PDF Reader . |
|
Datasheet File OCR Text: |
april 2015 doc id16895 rev 2 1/24 1 AN3126 application note audio and waveform generation using the dac in stm32 microcontrollers introduction this application note provides some exampl es for generating audio waveforms using the digital to analog converter (dac) peripheral embedded in the microcontrollers of the stm32fx and stm32lx series. this document applies to products listed in table 1 , and should be read in connection with application note an4566 ?extending the dac performance of stm32 microcontrollers?. a digital to analog converter, dac, is a device that has the opposite f unction to that of an analog to digital converter, i.e. it converts a digital word to a corresponding analog voltage. the stm32 dac module is a 12-bit word converter, with up to three output channels to support audio functions. the dac can be used in many audio applicat ions such as security alarms, bluetooth headsets, talking toys, answering machines, man-machine interfaces, and low-cost music players stm32 dac can also be used for many other analog purposes, such as analog waveform generation and control engineering. the application note is organized in two main sections: ? section 1 describes the main features of the stm32 dac module. ? section 2 presents two examples. ? in the first example, the dac is used to generate a sine wavefom. ? in the second example, the dac is used to generate audio from .wav files. table 1. applicable products type product series microcontrollers stm32f0 stm32f1 stm32f2 stm32f3 stm32f4 stm32f7 stm32l0 stm32l1 stm32l4 www.st.com
contents AN3126 2/24 doc id16895 rev 2 contents 1 dac main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.1 overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.2 data format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.3 dual channel mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.4 dedicated timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.5 dma capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.6 dma underrun error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.7 white noise generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.7.1 definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.7.2 typical applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.8 triangular wave generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 1.8.1 definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.8.2 typical applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.9 buffered output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2 application examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.1 using the dac to generate a sine waveform . . . . . . . . . . . . . . . . . . . . . . 15 2.1.1 description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.1.2 digital sine waveform pattern preparation . . . . . . . . . . . . . . . . . . . . . . . 15 2.1.3 fixing the sine wave frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.2 using the dac to implement an audio wave player . . . . . . . . . . . . . . . . . 18 2.2.1 description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.2.2 audio wave file specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.2.3 .wav file format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.3 audio wave player implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3 conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 4 revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 doc id16895 rev 2 3/24 AN3126 list of tables 3 list of tables table 1. applicable products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 table 2. dac configurations for stm32 microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 table 3. preprogrammable triangular waveform amplitude values . . . . . . . . . . . . . . . . . . . . . . . . . . 12 table 4. digital and analog sample values of the sine wave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 table 5. document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 list of figures AN3126 4/24 doc id16895 rev 2 list of figures figure 1. dac data format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 figure 2. stm32f100x dac trigger channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 figure 3. dac interaction without dma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 figure 4. dac interaction with dma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 figure 5. pseudo random code generator embedded in the da c . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 figure 6. noise waveform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 figure 7. noise waveform with changeable offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 figure 8. triangular wave form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 figure 9. triangular waveform with changeab le offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 figure 10. non buffered channel voltage (with and without load ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 figure 11. buffered channel voltage (with and without load) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 figure 12. sine wave model samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 figure 13. sine wave generated with ns = 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 figure 14. sine wave generated with ns = 255 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 figure 15. flow of data from microsd flash memory to ex ternal speakers . . . . . . . . . . . . . . . . . . . . 18 figure 16. wave player flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 figure 17. cpu and dma activities during wave playing proces s . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 doc id16895 rev 2 5/24 AN3126 dac main features 23 1 dac main features 1.1 overview stm32 microcontrollers integrate dac with different configurations and features: ? 1 to 3 dac output channels ? noise waveform generation ? triangular waveform generation ? dma under run flag ? dedicated analog clock table 2 summarizes the different stm32 dac configuration. table 2. dac configurations for stm32 microcontrollers series product rpn dac outputs white noise generator triangular wave generator dma capability dma underrun error f0 stm32f030xx stm32f031xx stm32f038xx stm32f042xx stm32f048xx stm32f070xx 0- - -- stm32f051xx stm32f058xx 1no noyesno stm32f071xx stm32f072xx stm32f078xx stm32f091xx stm32f098xx 2 yes yes yes yes f1 stm32f101x4/6/8b stm32f102xx stm32f103x4/6/8b 0- - -- stm32f100xx stm32f101xc/d/e/f/g stm32f103xc/d/e/f/g stm32f105xx stm32f107xx 2 yes yes yes yes f2 stm32f2xxxx 2 yes yes yes yes dac main features AN3126 6/24 doc id16895 rev 2 f3 stm32f301xx stm32f302xx stm32f318xx 1 yes yes yes yes stm32f303xb/c/d/e stm32f358xx stm32f398xx 2 yes yes yes yes stm32f3328 stm32f3334 stm32f3373 stm32f3378 3 yes (only for 2 channels) yes (only for 2 channels) yes yes f4 stm32f401xx stm32f411xx 0- - -- stm32f405xx stm32f407xx stm32f415xx stm32f417xx stm32f427xx stm32f429xx stm32f437xx stm32f439xx stm32f446xx 2 yes yes yes yes f7 stm32f7xxxx 2 yes yes yes yes l0 stm32l031xx stm32l041xx stm32l051xx stm32l071xx stm32l081xx 0- - -- stm32l052xx stm32l053xx stm32l062xx stm32l063xx 1 yes yes yes yes l1 stm32l1xxxx 2 yes yes yes yes l4 stm32l4xxxx 2 yes yes yes yes table 2. dac configurations for stm32 microcontrollers (continued) series product rpn dac outputs white noise generator triangular wave generator dma capability dma underrun error doc id16895 rev 2 7/24 AN3126 dac main features 23 1.2 data format the dac accepts data in 3 integer formats: 8-bit, 12-bit right aligned and 12-bit left aligned. a 12-bit value can range from 0x000 to 0x fff, with 0x000 being the lowest and 0xfff being the highest value. figure 1. dac data format 1.3 dual channel mode note: this feature is supported only for products that embed at least 2 dacs. the dac has two output channels, each with it s own converter. in dual dac channel mode, conversions could be done independently or simultaneously. when the dac channels are triggered by the same source, both channels are grouped together for synchronous update operations and conversions are done simultaneously. 1.4 dedicated timers in addition to the software and external triggers, the dac conversion can be triggered by different timers. tim6 and tim7 are basic timers and are basically designed for dac triggering. each time a dac interface detects a rising edge on the selected timer trigger output (timx_trgo), the last data stored in the dac_dhrx register is transferred to the dac_dorx register (an example for stm32f100x is given in figure 2 ). ai18300 dac main features AN3126 8/24 doc id16895 rev 2 figure 2. stm32f100x dac trigger channels 1.5 dma capabilities the stm32 microcontrollers have a dma module with multiple channels. each dac channel is connected to an independent dma channel. in the case of stm32f100x microcontrollers, the dac channel 1 is connec ted to the dma channel 3 and dac channel2 is connected to dma channel 4. when dma is not utilized, t he cpu is used to provide da c with the pattern waveform. generally the waveform is saved in a memory (ram), and the cpu is in charge of transferring the data from ram to the dac. figure 3. dac interaction without dma 0 6 9 ' $ & |